import axios from "axios";
import { Message } from "element-ui";

const instance = axios.create({
  baseURL: "/api",
  timeout: 5000,
});

instance.interceptors.response.use(
  (response) => {
    return new Promise((resolve, reject) => {
      const { code, message } = response;
      if (code === 404) {
        Message.error("404 资源不存在");
        reject(message);
      }
      resolve(response.data);
    });
  },
  (error) => {
    const { status, statusText } = error.response;
    if (status === 404) {
      Message.error("404 资源不存在");
    }

    return Promise.reject(error);
  }
);

export default (config) => {
  const { method, data } = config;
  console.log(data, method);
  const paylaodType = method == "get" ? "params" : "data";
  const params = {
    ...config,
    [paylaodType]: data,
  };
  return instance({ ...params });
};
